//
//  BusinessCertificationStep3Form.swift
//  iModern
//
//  Created by 张义帅 on 15/8/4.
//  Copyright (c) 2015年 com.starlooking. All rights reserved.
//

import UIKit
import XLForm

class BusinessCertificationStep3Form: FormViewController
{
    var contactProfile = NSMutableDictionary()
    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.navigationItem.title = "开设匠铺"
        self.initializeForm()
        self.initBackButton()
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }

    override func viewWillAppear(animated: Bool)
    {
        super.viewWillAppear(animated);
        MobClick.beginLogPageView("BusinessCertificationView3")

    }

    override func viewWillDisappear(animated: Bool)
    {
        super.viewWillDisappear(animated);
        MobClick.endLogPageView("BusinessCertificationView3")
    }

    func initializeForm()
    {
        var form:    XLFormDescriptor
        var section: XLFormSectionDescriptor
        var row:     XLFormRowDescriptor

        form = XLFormDescriptor(title: "")

        section = XLFormSectionDescriptor.formSectionWithTitle("您的匠铺所在地")
        form.addFormSection(section)

        row = XLFormRowDescriptor(tag: "address", rowType: XLFormRowDescriptorTypeText, title: "匠铺地址")
        row.cellConfigAtConfigure["textField.placeholder"] = "请填写您的匠铺地址"
        row.required = true
        section.addFormRow(row)

        section = XLFormSectionDescriptor.formSectionWithTitle("请准确填写以下信息")
        form.addFormSection(section)

        row = XLFormRowDescriptor(tag: "fullName", rowType: XLFormRowDescriptorTypeText, title: "姓名")
        row.cellConfigAtConfigure["textField.placeholder"] = "请填写您的真实姓名"
        row.required = true
        section.addFormRow(row)

        row = XLFormRowDescriptor(tag: "gender", rowType: XLFormRowDescriptorTypeSelectorPush, title: "性别")
        let a = ["男", "女"]
        row.selectorOptions = a
        section.addFormRow(row)

        row = XLFormRowDescriptor(tag: "city", rowType: XLFormRowDescriptorTypeText, title: "家乡")
        row.cellConfigAtConfigure["textField.placeholder"] = "请填写家乡"
        row.required = true
        section.addFormRow(row)

        section = XLFormSectionDescriptor.formSectionWithTitle("")
        form.addFormSection(section)
        row = XLFormRowDescriptor(tag: "complete", rowType: XLFormRowDescriptorTypeButton, title: "完成")
        row.cellConfigAtConfigure["tintColor"] = UIColor.whiteColor()
        row.cellConfigAtConfigure["backgroundColor"] = UIColor(red: 0.40784313725490196, green: 0.6588235294117647, blue: 0.19215686274509805, alpha: 1)
        row.action.formSelector = "completeTouchButton:"
        row.required = true
        section.addFormRow(row)

        self.form = form
    }

    func completeTouchButton(sender: XLFormRowDescriptor)
    {
        let address = self.formValues()["address"] as? String
        if address == nil
        {
            self.alertWarningMessage("请填写您的匠铺地址")
            return
        }

        let fullName = self.formValues()["fullName"] as? String
        if fullName == nil
        {
            self.alertWarningMessage("请填写您的姓名")
            return
        }

        let gender = self.formValues()["gender"] as? String
        if gender == nil
        {
            self.alertWarningMessage("请填写您的性别")
            return
        }

        let city = self.formValues()["city"] as? String
        if city == nil
        {
            self.alertWarningMessage("请填写您的家乡")
            return
        }

        self.contactProfile["address"] = self.formValues()["address"]
        self.contactProfile["fullName"] = self.formValues()["fullName"]
        self.contactProfile["gender"] = self.formValues()["gender"]
        self.contactProfile["city"] = self.formValues()["city"]
        self.contactProfile["type"] = "Shop"

        let rs = appDelegate.contactManager .updateProfile(contactProfile as [NSObject:AnyObject])
        if rs == nil
        {
            self.alertNetworkConnectionException()
        }

        if rs!.hasErrors()
        {
            if rs?.errorCode == 3020
            {
                alertUserStatusException()
                NSNotificationCenter.defaultCenter().postNotificationName("Logout", object: nil)
                self.view.endEditing(true)
                navigationController?.popToRootViewControllerAnimated(true)
            }
            alertErrorMessage(rs!.errorMessage)
        }
        self.view.endEditing(true)
        MobClick.event("becomed the boss")

        appDelegate.contactManager.loadContact()
        NSNotificationCenter.defaultCenter().postNotificationName("Refresh Shop", object: nil)
        self.navigationController?.popToRootViewControllerAnimated(true)
    }
}
